import requests
from config.global_config import config
from utils.notification_tools.message_template import Template
from utils.logging_tools.log_control import INFO,ERROR
from utils.other_tools.allure_report_data import AllureFileClean

def send_dingtalk_message(report_url: str):
    """
    发送钉钉机器人消息
    """
    url = config.dingtalk.webhook
    headers = {'Content-Type': 'application/json'}
    
    # 获取模板内容
    template_content = Template(AllureFileClean().get_case_count(),report_url=report_url).get_template
    
    # 构建钉钉机器人所需的JSON格式数据
    data = {
        "msgtype": "text",
        "text": {
            "content": template_content
        }
    }
    
    # 发送请求
    response = requests.post(url, json=data, headers=headers)

    # 检查响应
    if response.status_code == 200:
        result = response.json()
        if result.get("errcode") == 0:
            INFO.info("钉钉消息发送成功")
        else:
            ERROR.error(f"钉钉消息发送失败，错误信息: {result.get('errmsg')}")
    else:
        print(f"钉钉消息请求失败，状态码: {response.status_code}")
        return None


if __name__ == '__main__':
    send_dingtalk_message(report_url='1111111')